﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Net;
using System.IO;

namespace Food
{
	public partial class MainForm : Form
	{
		private WeekMenu _menu;
		private Week _week;

		private UserManager _userManager;

		public MainForm()
		{
			InitializeComponent();

			_userManager = new UserManager();

		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			FillUsersCombo();

			_week = new Week(dateTimePicker.Value);

			LoadMenu();

			FillCombo();

		}

		public void FillUsersCombo()
		{
			comboBoxUsers.Items.Clear();

			string pleaseSelect = "Please select...";

			if (_userManager.CurrentUser == string.Empty)
			{
				comboBoxUsers.Items.Add(pleaseSelect);
			}

			foreach (User user in _userManager.Users)
			{
				comboBoxUsers.Items.Add(user.DisplayName);
			}

			if (_userManager.CurrentUser == string.Empty)
			{
				comboBoxUsers.SelectedItem = pleaseSelect;
			}
			else
			{
				comboBoxUsers.SelectedItem = _userManager.CurrentUser;
			}
		}

		public void FillCombo()
		{
			//DayMenu dm = new DayMenu(null);

			//comboBoxSoups.Items.Clear();

			_menu.DailyMenus[DayOfWeek.Monday].Categories[0].FillCombo(comboBoxSoups);
			_menu.DailyMenus[DayOfWeek.Monday].Categories[1].FillCombo(comboBoxSalads);
			_menu.DailyMenus[DayOfWeek.Monday].Categories[2].FillCombo(comboBoxMainCourse);
			_menu.DailyMenus[DayOfWeek.Monday].Categories[3].FillCombo(comboBoxGarnish);
			_menu.DailyMenus[DayOfWeek.Monday].Categories[4].FillCombo(comboBoxSuperCourse);

// 			foreach(Food item in dm.Categories)
// 			{
// 				comboBoxSoups.Items.Add(item.Name);
// 			}

//			comboBoxSoups.DataSource = dm.MenuItems;
	//		comboBoxSoups.DisplayMember = "Price";
			//dm.MenuItems
		}
		
		public void LoadMenu()
		{
			_menu = new WeekMenu(_week);
		}

		public void FillMenu(DayMenu dayMenu)
		{
			comboBoxSoups.Items.Clear();

			List<Food> items = dayMenu.GetMenuItemsOfCategory("soups");

			foreach(Food item in items)
			{
				comboBoxSoups.Items.Add(item.Name + "	" + item.Price);
			}
		}

		private void RecalculateTotal()
		{
			int total = 0;

			foreach (ListViewItem item in listViewDayOrder.Items)
			{
				string a = item.SubItems[1].Text;
				total += Convert.ToInt32(a);
			}

			textBoxTotal.Text = total.ToString();
		}

#region Event Handlers

		private void buttonAddSoup_Click(object sender, EventArgs e)
		{
			//listViewDayOrder.Groups["listViewGroupSoups"]

			Food food = (Food)comboBoxSoups.SelectedItem;

			ListViewItem newItem = new ListViewItem(food.Name, listViewDayOrder.Groups["listViewGroupSoups"]);
			newItem.SubItems.Add(food.Price.ToString());
			listViewDayOrder.Items.Add(newItem);

			RecalculateTotal();
		}

		private void buttonAddSalad_Click(object sender, EventArgs e)
		{
			Food food = (Food)comboBoxSalads.SelectedItem;

			ListViewItem newItem = new ListViewItem(food.Name, listViewDayOrder.Groups["listViewGroupSalads"]);

			newItem.SubItems.Add(food.Price.ToString());

			listViewDayOrder.Items.Add(newItem);

			RecalculateTotal();
		}

		private void dateTimePicker_ValueChanged(object sender, EventArgs e)
		{
			
		}

		private void comboBoxUsers_SelectedIndexChanged(object sender, EventArgs e)
		{
			_userManager.CurrentUser = comboBoxUsers.SelectedItem.ToString();
		}

#endregion

		

	}
}
